package com.hyhy.hycloud.uaa.client;

import lombok.AllArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 用户信息参数解析
 *
 * @author Xuegui Yuan
 * @date 2018-09-25
 */
@AllArgsConstructor
public class UserInfoArgumentResolver implements HandlerMethodArgumentResolver {

    private UserInfoResourceService userInfoResourceService;

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(CurrentUserInfo.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) {
        CurrentUserInfo currentUserInfo = methodParameter.getParameterAnnotation(CurrentUserInfo.class);
        return userInfoResourceService.loadUserInfo(currentUserInfo, methodParameter.getParameterType(), nativeWebRequest);
    }
}
